//
//  LAWriteReviewScoreTableViewCell.m
//  LYLazizDevelopDemo
//
//  Created by xbn_iMac on 2016/11/3.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LAWriteReviewScoreTableViewCell.h"
#import "LAWriteReviewModel.h"

@implementation LAWriteReviewScoreTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.arrButtons = [NSMutableArray array];
    for (int i = 0; i<5; i++) {
        UIButton *btn = [self.contentView viewWithTag:i+10];
        [self.arrButtons addObject:btn];
    }
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setModel:(LAWriteReviewModel *)model {
    _model = model;
    
    NSInteger tag = 0;
    
    switch (self.indexPath.row) {
        case 0:
        {
            tag = model.restaurant_rating;
        }
            break;
        case 1:
        {
            tag = model.restaurant_taste;
        }
            break;
        case 2:
        {
            tag = model.restaurant_atmopsphere;
        }
            break;
        case 3:
        {
            tag = model.restaurant_service;
        }
            break;
            
        default:
            break;
    }
    
    for (int i = 0; i<5; i++) {
        UIButton *btn = self.arrButtons[i];
        
        if (tag == 0) {
            btn.selected = NO;
            break;
        }
        
        if (btn.tag <= tag+9) {
            btn.selected = YES;
        }
        else if (btn.tag > tag+9) {
            btn.selected = NO;
        }
    }
}

- (IBAction)reviewScoreButtonClickAction:(UIButton*)sender {
    if (sender.tag == 10) {
        sender.selected = YES;
    }
    else {
        sender.selected = !sender.selected;
    }
    
    for (int i = 0; i<5; i++) {
        UIButton *btn = self.arrButtons[i];
        if (btn.tag < sender.tag) {
            btn.selected = YES;
        }
        else if (btn.tag > sender.tag) {
            btn.selected = NO;
        }
    }
    
    if (sender.selected) {
        // 如果当前按钮是选中的
        [self refreshMode:sender.tag-9];
    }
    else {
        [self refreshMode:sender.tag-9-1];
    }
}

-(void)refreshMode:(NSInteger)tag {
    if (tag < 0) {
        tag = 0;
    }
    
    if (self.isVideoWriteReviewType) {
        self.model.video_rating = tag;
        return;
    }
    
    switch (self.indexPath.row) {
        case 0:
        {
            self.model.restaurant_rating = tag;
        }
            break;
        case 1:
        {
            self.model.restaurant_taste = tag;
        }
            break;
        case 2:
        {
            self.model.restaurant_atmopsphere = tag;
        }
            break;
        case 3:
        {
            self.model.restaurant_service = tag;
        }
            break;
            
        default:
            break;
    }
    
}

@end
